package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import control.ControlMain;
import dao.Studentdao;
import entity.Student;

public class DelFrame extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private JTextField idjtf;
	private ControlMain cm;
	private Studentdao sd;
	private Student s;
	private DefaultTableModel dtm;

	public DelFrame() {
		setTitle("删除学生信息");
		setVisible(true);
		setSize(500, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(false);
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(10, 20, 10, 20);
		JPanel top = new JPanel();
		dtm=new DefaultTableModel(new Object[][]{}, new String[]{"ID","姓名","性别"});
		table = new JTable(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane jsp = new JScrollPane(table);
		jsp.setPreferredSize(new Dimension(228, 199));
		gbc.gridy = 1;
		top.add(jsp);
		add(top, gbc);
		JPanel middle = new JPanel();
		middle.setLayout(new BorderLayout());
		JLabel idjl = new JLabel("请输入删除学生的id号:");
		idjl.setPreferredSize(new Dimension(150, 30));
		idjtf = new JTextField();
		idjtf.setPreferredSize(new Dimension(100, 30));
		JButton deljb = new JButton("删除");
		deljb.addActionListener(this);
		middle.add(idjl, BorderLayout.WEST);
		middle.add(idjtf);
		middle.add(deljb, BorderLayout.EAST);
		gbc.gridy = 2;
		add(middle, gbc);
		String[] str = { "返回", "关闭" };
		JButton[] jbt = new JButton[str.length];
		for (int i = 0; i < str.length; i++) {
			jbt[i] = new JButton(str[i]);
			jbt[i].setSize(80, 40);
			jbt[i].addActionListener(this);
			gbc.gridx = i;
			gbc.gridy = 3;
			add(jbt[i], gbc);
		}
		s = new Student();
		sd=new Studentdao();
		selStu();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd == "删除") {
			int f = JOptionPane.showConfirmDialog(this, "确认删除id为：" + idjtf.getText() + "学生信息吗？", "提示信息！",
					JOptionPane.YES_NO_OPTION);
			if (f == 0) {
				delStu();
				JOptionPane.showMessageDialog(this, "删除id为：" + idjtf.getText() + "学生信息成功！");
			} else {
				JOptionPane.showMessageDialog(this, "你已取消删除id为：" + idjtf.getText() + "学生信息！");
			}
		}
		if (cmd == "返回") {
			cm = new ControlMain();
			this.setVisible(false);
			cm.setVisible(true);
		}
		if (cmd == "关闭") {
			System.exit(0);
		}
	}

	public void selStu() {
		List<Student> stus = sd.selAll();
		for (Student stuinfo : stus) {
			dtm.addRow(new Object[]{String.valueOf(stuinfo.getId()),stuinfo.getName(),stuinfo.getSex()});
		}

	}

	public void delStu() {
		s.setId(Integer.valueOf(idjtf.getText()));
		sd.Delete(s);
	}

}
